package com.weimob.mcloud.wechatwork.customer.export;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.weimob.mcloud.privacy.starter.service.PrivacyLimitsClient;
import com.weimob.mcloud.wechatwork.customer.export.sentinel.SentinelCommonBlockHandler;
import com.weimob.mcloud.wechatwork.customer.facade.impl.CommonValid;
import com.weimob.mcloud.wechatwork.customer.facade.impl.customer.CustomerFacade;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BaseRequest;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PageResponse;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BaseErrorCode;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.*;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.openapi.CustomerBaseInfoApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.*;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.openapi.CustomerDetailApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.openapi.CustomerQueryApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.openapi.ExternalUserInfoApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.openapi.ExternalUserInfoByWidsApi;
import com.weimob.mcloud.wechatwork.customer.service.user.service.CustomerExport;
import com.weimob.mcloud.wechatwork.customer.service.user.utils.SoaResponseUtil;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author yu liu  2021/10/25 17:42
 * Description
 * Version 1.0.0
 **/
@Slf4j
@Service
public class CustomerExportImpl implements CustomerExport {


    @Autowired
    private CustomerFacade customerFacade;

    @Autowired
    private CommonValid commonValid;

    @Override
    public SoaResponse<CustomerPageResponse<ExternalUserListBO>, ?> queryExternalUserBaseInfoList(
            ExternalUserInfoFindDTO externalUserInfoListDTO) {
        //校验分页参数
        commonValid.checkESWindowSize(externalUserInfoListDTO.getPageNum(), externalUserInfoListDTO.getPageSize());
        return SoaResponseUtil.ok(customerFacade.queryExternalUserBaseInfoList(externalUserInfoListDTO));
    }

    @Override
    public SoaResponse<Boolean, ?> exportExternalUserList(ExternalUserInfoFindDTO externalUserInfoListDTO) {
        return SoaResponseUtil.ok(customerFacade.exportExternalUserList(externalUserInfoListDTO));
    }


    @Override
    public SoaResponse<PageResponse<ExternalUserListBO>, ?> getExternalUserByWids(
            ExternalUserInfoFindDTO externalUserInfoListDTO) {
        return SoaResponseUtil.ok(customerFacade.getExternalUserByWids(externalUserInfoListDTO));
    }

    @Override
    public SoaResponse<List<ExternalUserRelationBO>, ?> getExternalUserRelationByWids(ExternalUserInfoByWidsDTO externalUserInfoByWidsDTO) {
        return SoaResponseUtil.ok(customerFacade.getExternalUserRelationByWids(externalUserInfoByWidsDTO));
    }

    /**
     * 查询外部联系人所属员工列表
     *
     * @param customerDetailDTO
     * @return
     */
    @Override
    public SoaResponse<PageResponse<ExternalUserRelationBO>, ?> getExternalUserRelationById(CustomerDetailDTO customerDetailDTO) {
        return SoaResponseUtil.ok(customerFacade.getEmployeeByCustomerId(customerDetailDTO));
    }

    @Override
    public SoaResponse<PageResponse<CustomerActionLogBO>, ?> findCorpActionLog(CorpActionLogFindDTO dto) {
        return SoaResponseUtil.ok(customerFacade.findCorpActionLog(dto));
    }

    @Override
    public SoaResponse<CustomerActionLogBO, ?> addActionLog(CorpActionLogInsertDTO dto) {
        return SoaResponseUtil.ok(customerFacade.addActionLog(dto));
    }

    @Override
    @SentinelResource(value = "queryCustomerDetail", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "queryCustomerDetailBlockHandler")
    public SoaResponse<CustomerDetailBO, ?> queryCustomerDetail(CustomerDetailDTO queryDTO) {
        return SoaResponseUtil.ok(customerFacade.queryCustomerDetail(queryDTO));
    }

    @Override
    public SoaResponse<PageResponse<CustomerLogBO>, ?> findCustomerLog(CustomerLogFindDTO dto) {
        return SoaResponseUtil.ok(customerFacade.findCustomerLog(dto));
    }

    @Override
    public SoaResponse<List<DivisionBO>, ?> findDivisionList(DivisionFindDTO divisionFindDTO) {
        return SoaResponseUtil.ok(customerFacade.findDivisionList(divisionFindDTO));
    }

    @Override
    public SoaResponse<List<CustomerSearchCallbackBO>, ?> customerSearchCallback(
            CustomerSearchCallbackDTO customerSearchCallbackDTO) {
        return SoaResponseUtil.ok(customerFacade.customerSearchCallback(customerSearchCallbackDTO));
    }

    @Override
    @SentinelResource(value = "getCustomerBaseInfo", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "getCustomerBaseInfoBlockHandler")
    public SoaResponse<CustomerDetailBO, ?> getCustomerBaseInfo(CustomerIdGetDTO customerIdGetDTO) {
        return SoaResponseUtil.ok(customerFacade.getCustomerBaseInfo(customerIdGetDTO, null));
    }

    @Override
    @SentinelResource(value = "getCustomerBaseInfoList", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "getCustomerBaseInfoListBlockHandler")
    public SoaResponse<List<CustomerDetailBO>, ?> getCustomerBaseInfoList(CustomerIdGetDTO customerIdGetDTO) {
        return SoaResponseUtil.ok(customerFacade.getCustomerBaseInfoList(customerIdGetDTO, null));
    }

    @Override
    public SoaResponse<PageResponse<CustomerDetailBO>, ?> getCustomerListByEmployee(CustomerByEmployeeDTO customerByEmployeeDTO) {
        return SoaResponseUtil.ok(customerFacade.getCustomerListByEmployee(customerByEmployeeDTO));
    }

    @Override
    public SoaResponse<Boolean, ?> triggerSyncCustomer(SyncCustomerTriggerDTO syncCustomerTriggerDTO) {
        return SoaResponseUtil.ok(customerFacade.triggerSyncCustomer(syncCustomerTriggerDTO));
    }

    @Override
    public SoaResponse<Boolean, ?> updateCustomerRemark(CustomerRemarkDTO customerRemarkDTO) {
        return SoaResponseUtil.ok(customerFacade.updateCustomerRemark(customerRemarkDTO));
    }

    @Override
    public SoaResponse<Boolean, ?> saveCustomerCDPDetail(CustomerCDPInfoSaveDTO customerCDPInfoSaveDTO) {
        return SoaResponseUtil.ok(customerFacade.saveCustomerCDPDetail(customerCDPInfoSaveDTO));
    }

    @Override
    public SoaResponse<SyncHistoryExternaluserBO, ?> checkSyncHistoryCustomer(BaseRequest request) {
        return SoaResponseUtil.ok(customerFacade.checkSyncHistoryCustomer(request));
    }

    @Override
    public SoaResponse<Boolean, ?> syncHistoryCustomer(BaseRequest request) {
        return SoaResponseUtil.ok(customerFacade.syncHistoryCustomer(request));
    }

    @Override
    public SoaResponse<PageResponse<ExternalUserExportListBO>, Void> getExportExternalUserInfo(ExternalUserInfoFindDTO externalUserInfoFindDTO) {
        return SoaResponseUtil.ok(new PageResponse<>());
    }

    @Override
    public SoaResponse<Boolean, ?> sendCustomerSearchMsg(CustomerSearchMsgDTO request) {
        return SoaResponseUtil.ok(customerFacade.sendCustomerSearchMsg(request));
    }


    @Override
    public SoaResponse<Boolean, ?> mergeCustomerWid(List<CustomerWidMergeDTO> dtoList) {
        return SoaResponseUtil.ok(customerFacade.mergeCustomerWid(dtoList));
    }

    @Override
    public SoaResponse<PageResponse<ExternalUserListBO>, ?> getExternalUserBySearch(
            ExternalUserInfoFindDTO externalUserInfoListDTO) {
        return SoaResponseUtil.ok(customerFacade.getExternalUserBySearch(externalUserInfoListDTO));
    }

    @Override
    public SoaResponse<List<CustomerAddWayBO>, ?> getCustomerAddWayEnum(BaseRequest request) {
        return SoaResponseUtil.ok(customerFacade.getCustomerAddWayEnum(request));
    }

    @Override
    public SoaResponse<PageResponse<CustomerCouponsBO>, ?> getCustomerCoupons(CustomerCouponsDTO customerCDPInfoSaveDTO) {
        return SoaResponseUtil.ok(customerFacade.getCustomerCoupons(customerCDPInfoSaveDTO));
    }

    @Override
    public SoaResponse<List<ExternalUserListBO>, ?> getCustomerByWids(ExternalUserInfoByWidsDTO externalUserInfoByWidsDTO) {
        return SoaResponseUtil.ok(customerFacade.getCustomerByWids(externalUserInfoByWidsDTO));
    }

    @Override
    @SentinelResource(value = "queryExternalUserList", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "queryExternalUserListBlockHandler")
    public SoaResponse<CustomerPageResponse<ExternalUserListBO>, ?> queryExternalUserList(ExternalUserInfoApi api) {
        //校验分页参数
        commonValid.checkESWindowSize(api.getPageNum(), api.getPageSize());
        //入参转换
        ExternalUserInfoFindDTO findDTO = new ExternalUserInfoFindDTO();
        BeanUtils.copyProperties(api, findDTO);
        findDTO.setBasicInfo(api.convertToBasicInfo());
        return SoaResponseUtil.ok(customerFacade.queryExternalUserBaseInfoList(findDTO));
    }

    @Override
    public SoaResponse<CustomerDetailBO, ?> queryCustomerDetail(CustomerDetailApi api) {
        //入参转换
        CustomerDetailDTO customerDetailDTO = new CustomerDetailDTO();
        BeanUtils.copyProperties(api, customerDetailDTO);
        customerDetailDTO.setBasicInfo(api.convertToBasicInfo());
        return SoaResponseUtil.ok(customerFacade.queryCustomerDetail(customerDetailDTO));
    }

    @Override
    @SentinelResource(value = "queryCustomerRelation", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "queryCustomerRelationBlockHandler")
    public SoaResponse<List<CustomerRelationBO>, ?> queryCustomerRelation(ExternalUserInfoByWidsApi api) {
        if (api.getWid() == null) {
            throw new BizException(BaseErrorCode.PARAM_WID_NULL_EXCPETION.getCode(), BaseErrorCode.PARAM_WID_NULL_EXCPETION.getMsg());
        }
        //入参转换
        ExternalUserInfoByWidsDTO externalUserInfoByWidsDTO = new ExternalUserInfoByWidsDTO();
        externalUserInfoByWidsDTO.setUserId(api.getUserId());
        externalUserInfoByWidsDTO.setWids(Collections.singletonList(api.getWid()));
        externalUserInfoByWidsDTO.setStatus(api.getStatus());
        externalUserInfoByWidsDTO.setBasicInfo(api.convertToBasicInfo());
        List<ExternalUserRelationBO> externalUserRelationByWids = customerFacade.getExternalUserRelationByWids(externalUserInfoByWidsDTO);
        List<CustomerRelationBO> customerRelationBOS = new ArrayList<>();
        for (ExternalUserRelationBO externalUserRelationBO : externalUserRelationByWids) {
            CustomerRelationBO customerRelationBO = new CustomerRelationBO();
            BeanUtils.copyProperties(externalUserRelationBO, customerRelationBO);
            customerRelationBOS.add(customerRelationBO);
        }

        return SoaResponseUtil.ok(customerRelationBOS);
    }

    @Override
    public SoaResponse<Boolean, ?> checkIsFriended(CustomerDetailDTO customerDetailDTO) {
        return SoaResponseUtil.ok(customerFacade.checkIsFriended(customerDetailDTO));
    }

    /**
     * 查询企微客户基础信息 通过wid或者externalUserId
     *
     * @param queryDTO 入参对象
     * @return SoaResponse<CustomerDetailBO, ?> 返回对象
     */
    @Override
    public SoaResponse<CustomerBaseInfoApi, ?> queryCustomerBaseInfoForApi(CustomerQueryApi queryDTO) {
        return SoaResponseUtil.ok(customerFacade.queryCustomerBaseInfoForApi(queryDTO));
    }
}
